package com.servlet;

import com.beans.Memberinfo;
import com.beans.PageInfo;
import com.dao.MemberDao;
import com.jdbc.PageUtil;
import org.omg.PortableInterceptor.ServerRequestInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MemberServlet")
public class MemberServlet extends HttpServlet {
    Memberinfo memberinfo=new Memberinfo();
    MemberDao memberDao=new MemberDao();
    PageInfo pageInfo=new PageInfo();
    PageUtil pageUtil=new PageUtil();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String flag = request.getParameter("flag");
        if ("manage".equals(flag)){
            manage(request,response);
        }else if ("srea".equals(flag)){
            srea(request,response);
        }else if("del".equals(flag)){
            del(request,response);
        }else if("duoshan".equals(flag)){
            duoshan(request,response);
        }
    }

    private void duoshan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idlist=request.getParameter("id");
        String sid[]= idlist.split(" ");
        for(int i=0;i<sid.length;i++){
            memberDao.del(Integer.parseInt(sid[i]));
            System.out.println(sid[i]);
        }
        manage(request,response);
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        int i=memberDao.del(id);
        if(i>0){
            manage(request,response);
        }
    }

    private void srea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Memberinfo memberinfo=memberDao.getbyid(id);
        request.setAttribute("memberinfo",memberinfo);
        request.getRequestDispatcher("member/member-edit.jsp").forward(request, response);
    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sreaName=request.getParameter("sreaName");
        String startDate=request.getParameter("startDate");
        String endDate=request.getParameter("endDate");
        int pageIndex=1;
        int pageSize=6;
        int rowCount=memberDao.getmemberCount(sreaName,startDate,endDate);

        String pageIndexStr=request.getParameter("pageIndex");
        if(pageIndexStr!=null) {
            pageIndex=Integer.parseInt(pageIndexStr);
        }
        PageInfo page= PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<Memberinfo> mlist= memberDao.getMemberList(page,sreaName,startDate,endDate);
        request.setAttribute("sreaName",sreaName);
        request.setAttribute("startDate",startDate);
        request.setAttribute("endDate",endDate);
        request.setAttribute("mlist", mlist);
        request.setAttribute("page", page);
        request.getRequestDispatcher("member/member-manage.jsp").forward(request, response);
    }
}
